const workRecord = require("../models/workRecords")

var express = require('express');
var router = express.Router();

const formidable = require('formidable');

//返回图片
router.post('/', (req, res, next) => {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        if (err) {
            res.json({ 'state': false })
        } else {
          workRecord.find({},{
              time: 1
            }, async (error, doc) => {
                if (error) {
                    res.json({ 'state': false, 'msg': error });
                } else {
                    let count = 0;
                    // let time =[];
                    console.log(doc.length)
                    for(let i=0;i<doc.length;i++){
                        // console.log(doc[i].process[0].html);
                        // if(doc[i].process[0].html==undefined){
                        //     // console.log(doc[i].process.length);
                        //     // count++;
                        //     let update = doc[i].process[0];
                        //     update['html'] = "";
                        //     update['css'] = "";
                        //     update['js'] = "";
                        //     let new_process = [];
                        //     new_process.push(update);
                        //     // console.log(update);
                        await workRecord.updateOne({_id: doc[i]._id}, {
                                $set:{
                                    time: new Date(doc[i].time)
                                }
                            }, (err, data) => {
                                if(err) {
                                    res.json({state: false, msg: err})
                                }else {
                                    count++;
                                }
                            })
                        // }
                        // time.push(new Date(doc[i].time))
                        // console.log(new Date(doc[i].time))
                    }
                    res.json({ 'state': true, 'msg': count });
                }
            })

        }

    });

});

module.exports = router;